﻿using Hongsh.Pay.Client.Proxy.Http;
using MapleLeaf.Pay.Pay;
using MapleLeaf.Pay.Trade;
using System;

namespace Hongsh.Pay.Client.Proxy.Handlers {
    // Token: 0x02000057 RID: 87
    public static class JWPayProxy {
        // Token: 0x060002DE RID: 734 RVA: 0x0000DFC4 File Offset: 0x0000C1C4
        public static void AddJWPayProxyHandler(this HttpServer svr) {
            svr.RouterList.Add("GET/index.pnp", new HttpContextActionDelegate(Init));
            svr.RouterList.Add("POST/index.pnp", new HttpContextActionDelegate(Init));
            svr.RouterList.Add("POST/", new HttpContextActionDelegate(Process));
        }

        // Token: 0x060002DF RID: 735 RVA: 0x00003CEC File Offset: 0x00001EEC
        private static void Init(HttpContext ctx) {
            ctx.Result = InitResult;
        }

        // Token: 0x060002E0 RID: 736 RVA: 0x0000E028 File Offset: 0x0000C228
        private static void Process(HttpContext ctx) {
            JWPayResult jwpayResult = null;
            try {
                string a = ctx.Request.Form["service"];
                HongshPayTrade hongshPayTrade;
                if (a == "unipay.acquire.createandpay") {
                    hongshPayTrade = ctx.Execute(new TradeAuthPaymentRequest {
                        Amount = (int)(decimal.Parse(ctx.Request.Form["total_fee"]) * 100m),
                        Authcode = ctx.Request.Form["dynamic_id"]
                    });
                }
                else {
                    if (!(a == "unipay.acquire.query")) {
                        throw new Exception("不支持的操作！");
                    }
                    hongshPayTrade = ctx.Execute(new TradePaymentQueryRequest {
                        TradeId = ctx.Request.Form["trace_num"]
                    });
                }
                if (hongshPayTrade.State == TradeState.成功) {
                    jwpayResult = new GClass3 {
                        dynamic_type = (hongshPayTrade.Payway == TradePayway.微信) ? 1 : 2,
                        trace_num = hongshPayTrade.TradeId,
                        result_code = "SUCCESS",
                        result_msg = "OK",
                        input_fee = Math.Round(hongshPayTrade.Amount / 100m, 2),
                        total_fee = Math.Round(hongshPayTrade.ActualAmount / 100m, 2),
                        trans_time = hongshPayTrade.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                        trans_card_num = hongshPayTrade.Payer
                    };
                }
                else if (hongshPayTrade.State == TradeState.失败 || hongshPayTrade.State == TradeState.关闭) {
                    jwpayResult = new JWPayResult {
                        trace_num = hongshPayTrade.TradeId,
                        result_code = "PAYERROR",
                        result_msg = hongshPayTrade.StateDescript
                    };
                }
                else {
                    jwpayResult = new JWPayResult {
                        trace_num = hongshPayTrade.TradeId,
                        result_code = "PAYING",
                        result_msg = "等待用户处理中！"
                    };
                }
            }
            catch (Exception ex) {
                jwpayResult = new JWPayResult {
                    result_code = "FAIL",
                    result_msg = ex.Message
                };
            }
            string text;
            if (ctx.Request.Form.TryGetValue("merchant_num", out text)) {
                jwpayResult.merchant_num = text;
            }
            if (ctx.Request.Form.TryGetValue("terminal_num", out text)) {
                jwpayResult.terminal_num = text;
            }
            ctx.Result = new GClass0(jwpayResult, "xml");
        }

        // Token: 0x060002E1 RID: 737 RVA: 0x00003CF9 File Offset: 0x00001EF9
        static JWPayProxy() {
            //Class15.cwcZ3SWztHroi();
            InitResult = new HttpJsonResult(new {
                result_code = "SUCCESS",
                serial_no = "08601586316"
            });
        }

        // Token: 0x0400013D RID: 317
        private static readonly HttpResult InitResult;

        // Token: 0x02000058 RID: 88
        public class JWPayResult {
            // Token: 0x1700013E RID: 318
            // (get) Token: 0x060002E2 RID: 738 RVA: 0x00003D19 File Offset: 0x00001F19
            // (set) Token: 0x060002E3 RID: 739 RVA: 0x00003D21 File Offset: 0x00001F21
            public int dynamic_type { get; set; }

            // Token: 0x1700013F RID: 319
            // (get) Token: 0x060002E4 RID: 740 RVA: 0x00003D2A File Offset: 0x00001F2A
            // (set) Token: 0x060002E5 RID: 741 RVA: 0x00003D32 File Offset: 0x00001F32
            public string merchant_num { get; set; }

            // Token: 0x17000140 RID: 320
            // (get) Token: 0x060002E6 RID: 742 RVA: 0x00003D3B File Offset: 0x00001F3B
            // (set) Token: 0x060002E7 RID: 743 RVA: 0x00003D43 File Offset: 0x00001F43
            public string terminal_num { get; set; }

            // Token: 0x17000141 RID: 321
            // (get) Token: 0x060002E8 RID: 744 RVA: 0x00003D4C File Offset: 0x00001F4C
            // (set) Token: 0x060002E9 RID: 745 RVA: 0x00003D54 File Offset: 0x00001F54
            public string trace_num { get; set; }

            // Token: 0x17000142 RID: 322
            // (get) Token: 0x060002EA RID: 746 RVA: 0x00003D5D File Offset: 0x00001F5D
            // (set) Token: 0x060002EB RID: 747 RVA: 0x00003D65 File Offset: 0x00001F65
            public string result_code { get; set; }

            // Token: 0x17000143 RID: 323
            // (get) Token: 0x060002EC RID: 748 RVA: 0x00003D6E File Offset: 0x00001F6E
            // (set) Token: 0x060002ED RID: 749 RVA: 0x00003D76 File Offset: 0x00001F76
            public string result_msg { get; set; }

            // Token: 0x060002EE RID: 750 RVA: 0x00003D7F File Offset: 0x00001F7F
            public JWPayResult() {
                //Class15.cwcZ3SWztHroi();
                dynamic_type = 1;
                // base..ctor();
            }
        }

        // Token: 0x02000059 RID: 89
        public class GClass3 : JWPayResult {
            // Token: 0x17000144 RID: 324
            // (get) Token: 0x060002EF RID: 751 RVA: 0x00003D93 File Offset: 0x00001F93
            // (set) Token: 0x060002F0 RID: 752 RVA: 0x00003D9B File Offset: 0x00001F9B
            public string trans_type { get; set; }

            // Token: 0x17000145 RID: 325
            // (get) Token: 0x060002F1 RID: 753 RVA: 0x00003DA4 File Offset: 0x00001FA4
            // (set) Token: 0x060002F2 RID: 754 RVA: 0x00003DAC File Offset: 0x00001FAC
            public string trans_time { get; set; }

            // Token: 0x17000146 RID: 326
            // (get) Token: 0x060002F3 RID: 755 RVA: 0x0000E2B4 File Offset: 0x0000C4B4
            public string trans_amount {
                get {
                    return totalAmount.ToString("000000000000");
                }
            }

            // Token: 0x17000147 RID: 327
            // (get) Token: 0x060002F4 RID: 756 RVA: 0x00003DB5 File Offset: 0x00001FB5
            // (set) Token: 0x060002F5 RID: 757 RVA: 0x00003DBD File Offset: 0x00001FBD
            public string trans_card_num { get; set; }

            // Token: 0x17000148 RID: 328
            // (get) Token: 0x060002F6 RID: 758 RVA: 0x00003DC6 File Offset: 0x00001FC6
            public string trans_no {
                get {
                    return trace_num;
                }
            }

            // Token: 0x17000149 RID: 329
            // (get) Token: 0x060002F7 RID: 759 RVA: 0x00003DC6 File Offset: 0x00001FC6
            public string trade_no {
                get {
                    return trace_num;
                }
            }

            // Token: 0x1700014A RID: 330
            // (get) Token: 0x060002F8 RID: 760 RVA: 0x00003DC6 File Offset: 0x00001FC6
            public string out_trans_no {
                get {
                    return trace_num;
                }
            }

            // Token: 0x1700014B RID: 331
            // (get) Token: 0x060002F9 RID: 761 RVA: 0x00003DC6 File Offset: 0x00001FC6
            public string ali_trans_no {
                get {
                    return trace_num;
                }
            }

            // Token: 0x1700014C RID: 332
            // (get) Token: 0x060002FA RID: 762 RVA: 0x00003DCE File Offset: 0x00001FCE
            // (set) Token: 0x060002FB RID: 763 RVA: 0x00003DD6 File Offset: 0x00001FD6
            public decimal total_fee { get; set; }

            // Token: 0x1700014D RID: 333
            // (get) Token: 0x060002FC RID: 764 RVA: 0x00003DDF File Offset: 0x00001FDF
            // (set) Token: 0x060002FD RID: 765 RVA: 0x00003DE7 File Offset: 0x00001FE7
            public decimal input_fee { get; set; }

            // Token: 0x1700014E RID: 334
            // (get) Token: 0x060002FE RID: 766 RVA: 0x00003DF0 File Offset: 0x00001FF0
            public decimal coupon_fee {
                get {
                    return input_fee - total_fee;
                }
            }

            // Token: 0x1700014F RID: 335
            // (get) Token: 0x060002FF RID: 767 RVA: 0x00003E03 File Offset: 0x00002003
            public int totalAmount {
                get {
                    return (int)(input_fee * 100m);
                }
            }

            // Token: 0x06000300 RID: 768 RVA: 0x00003E1C File Offset: 0x0000201C
            public GClass3() {
                // Class15.cwcZ3SWztHroi();
                trans_type = "S1";
                //base..ctor();
            }
        }
    }
}
